/**
 * Created by Administrator on 2017/3/1 0001.
 */
function JsonOBJ() {
    this.id = null;
    this.majorname = null;
    this.classname = null;
    this.studentname = null;
    this.startTime = null;
    this.endTime = null;
    this.teachername = null;
    this.requestReason = null;
    this.rejectReason = null;
    this.authStatus = null;
    this.requestTime = null;
}

$(function () {
    var type = sessionStorage.getItem("type");
    var one_record = sessionStorage.getItem("one_record");
    getOneApply(one_record,type);
});

function getOneApply(id, type) {
    $.ajax({
        url: "/door/front/teacher/getOneApply?id=" + id + "&" + new Date().getTime(),
        type: "get",
        async: false,
        success: function (str) {
            if (check(str)) {
                if (str.id != null) {
                    setInfo(str, type);
                }
            }
        }
    });
}

function setInfo(str, type) {
    $("#stu_name").text(str.studentname);
    $("#class_name").text(str.classname);
    $("#startTime").val(DateUtils.format(new Date(str.startTime), "yyyy-MM-dd HH:mm:ss"));
    $("#endTime").val(DateUtils.format(new Date(str.endTime), "yyyy-MM-dd HH:mm:ss"));
    $("#apply-reason").val(str.requestReason);
    if (type == '0') {

        $(".apply-yes-we").click(function () {
            approveList(0);
            return false;
        });
        $(".apply-no-we").click(function () {
            layer.prompt({title: '请填写拒绝理由', formType: 2}, function(text, index){
                layer.close(index);
                approveList(1,text);
            });
            return false;
        });
    } else {
        $(".apply-yes-we").hide();
        $(".apply-no-we").text("返回");
        $(".apply-no-we").click(function () {
            window.location='apply.weHistory_teacher.html';
        });
    }
}

function approveList(type,text) {
    var checkIds = new Array();
    var js = new JsonOBJ();
    js.id = sessionStorage.getItem("one_record");
    if (type == '0') {
        js.authStatus = 1;
        js.rejectReason = "";
    } else {
        js.authStatus = 2;
        js.rejectReason = text;
    }
    checkIds.push(js);
    $.ajax({
        url: "/door/front/teacher/approve?" + new Date().getTime(),
        type: "post",
        data: JSON.stringify(checkIds),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (str) {
            if (check(str)) {
                if (str == checkIds.length) {
                    layer.msg("提交成功", {time: 1500});
                } else {
                    layer.msg("失败" + (checkIds.length - parseInt(str)) + "条！", {time: 1500});
                }
                setTimeout(function () {
                    window.location='apply.weApprove_teacher.html';
                },1500);
            }
        }
    });
}

function check(str) {
    if (str != null && str != "" || str == 0) {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    // window.location.href = "/door/front_end/pc/teacher/login_pc_teacher.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}